package com.bishe.supermarket.service;

import com.bishe.supermarket.mapper.StaffMapper;
import com.bishe.supermarket.pojo.Staff;
import com.bishe.supermarket.pojo.vo.Status;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StaffService {

    @Autowired
    StaffMapper staffMapper;
    //把数据库中的员工信息放在页面

    public List<Staff> selectAll(){

        List<Staff> staff = staffMapper.selectAll();

        return staff;
    }

    //通过员工姓名查找

    public List<Staff> selectByName(String name){

        List<Staff> list = staffMapper.selectByName(name);

        return list;
    }
    //插入员工信息
    public Status addStaff(String name,int age){
        Status status = new Status();

        if(StringUtils.isBlank(name)){
            status.setMsg("用户名不能为空！");
            status.setCode(false);
            return status;
        }
        if (age<0){
            status.setCode(false);
            status.setMsg("请输入合法年龄！");
            return status;
        }
        //判断该用户名是否存在，如果存在，则返回提示消息
        List<Staff> list = staffMapper.selectByName(name);

        if (!list.isEmpty()){
            status.setMsg("该用户已经存在！");
            status.setCode(false);
            return status;
        }

        Staff staff = new Staff();
        staff.setStaffAge(age);
        staff.setStaffName(name);
        //插入
         staffMapper.insert(staff);

         status.setCode(true);
         status.setMsg("添加成功！");

         return status;
    }

    //通过用户名删除用户

    public Status deleteStaff(String name){

        Status status = new Status();
//        if (StringUtils.isBlank(name)){
//            status.setCode(false);
//            status.setMsg("请输入合法的用户名！");
//            return status;
//        }

      staffMapper.delete(name);
      status.setMsg("删除成功！");
      status.setCode(true);

      return status;
    }
}
